/*
COPYRIGHT (C) 2008 EPISERVER AB

THIS FILE IS PART OF SCRUM DASHBOARD.

SCRUM DASHBOARD IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF 
THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

SCRUM DASHBOARD IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT
EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER
GENERAL PUBLIC LICENSE FOR MORE DETAILS.

YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH SCRUM DASHBOARD. 
IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ScrumDashboard.Library;
using ScrumDashboard.Library.Model;

namespace ScrumDashboard.ui
{
    public partial class WorkItemView : System.Web.UI.UserControl
    {
        private SprintBacklogItem _sb;
        private string _cssClass;
        private string _cssClassWithDragBox;

        protected void Page_PreRender(object sender, EventArgs e)
        {
            WorkItemPanel.Attributes.Add("sbId",SB.Id.ToString());
            WorkItemPanel.Attributes.Add("sbAssignedTo", SB.AssignedTo.ToString());
            WorkItemPanel.Attributes.Add("sbState", SB.State.Replace(" ", ""));
        }

        public SprintBacklogItem SB
        {
            get { return _sb; }
            set { _sb = value; }
        }

        public string CssClass
        {
            get { return _cssClass; }
            set { _cssClass = value; }
        }

        public string CssClassWithDragBox
        {
            get { return CssClass+" dragItem"; }
            set { _cssClassWithDragBox = value; }
        }

        protected string Clip(string title, int count, bool htmlEncode)
        {
            string retVal;

            if (title.Length > 0)
            {
                title = title.StartsWith(SprintBacklogItem.BugTitlePrefix) ? title.Substring(SprintBacklogItem.BugTitlePrefix.Length) : title;
            }
            if (title.Length == 0)
                retVal = "(empty)";
            else
            {
                retVal = title.Length > count ? title.Substring(0, count) + ".." : title;
            }
            return htmlEncode ? Server.HtmlEncode(retVal) : retVal;
        } 

        protected bool DisplayInBold()
        {
            if (Page.User!=null && String.Compare(Page.User.Identity.Name, SB.AssignedTo.Domain + @"\" + SB.AssignedTo.AccountName, true) == 0)
                return true;
            else
                return false;
        }

        protected string GetShortStatusInfo()
        {
            string assignedTo = String.IsNullOrEmpty(_sb.AssignedTo.DisplayName) ? String.Empty : Server.HtmlEncode(_sb.AssignedTo.DisplayName);
            string shortAssignedTo = String.IsNullOrEmpty(_sb.AssignedTo.Initials) ? String.Empty : Server.HtmlEncode(_sb.AssignedTo.Initials);

            shortAssignedTo = "<span title=\"" + assignedTo + "\" id=\"assignedTO" + _sb.Id + "\">" + shortAssignedTo + "</span>&nbsp;<span style=\"cursor:pointer;\" class=\"ChangeTime\" title=\"Change time for item\" onclick=\"db.changeTime(this, " + _sb.Id + ");\">" + _sb.WorkRemaining.ToString() + "</span>h";

            if (_sb.BacklogType == "Bug" || (_sb.Title!=null && _sb.Title.StartsWith("Bug:")))
                shortAssignedTo = "<img style=\"cursor:default;\" width=\"12\" height=\"12\" src=\"images/bug_red.png\"> " + shortAssignedTo;
            if (_sb.BacklogType == "Impediment")
                shortAssignedTo = "<img style=\"cursor:default;\" width=\"12\" height=\"12\" src=\"images/warning.png\"> " + shortAssignedTo;

            return shortAssignedTo;

        }

        protected string GetOpacity()
        {
            TimeSpan changed = DateTime.Now - SB.Changed;

            int totalHours = Convert.ToInt32(changed.TotalHours);

            // This check is important to compute the correct opacity, don't change 
            // without changing how we find out the opacity two lines below
            if (totalHours < 0 || totalHours > 49)
                return "0";

            //Assuming totalHours is never more than 49
            int opacity = 100 - Convert.ToInt32(totalHours * 2);

            return opacity < 10 ? "0" : opacity.ToString();
        }
    }
}